package com.ibm.ops.persistent;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.ibm.ops.conf.ProjectConfiguration;
import com.ibm.ops.conf.ProjectConfiguration.ProjectConfigurationDomain;
import com.ibm.ops.domain.OpsLogMetaDataType;
import com.ibm.ops.domain.metadata.OpsAuditLogReportMetaData;
import com.ibm.ops.exception.InvalidPropertiesException;
import com.ibm.ops.exception.NoPropConfigurationException;
import com.ibm.ops.exception.PersistentIOException;

public class XLSV03PersistentHandler implements PersistentHandler {


	public void persistent(List<OpsAuditLogReportMetaData> persistentContent) throws PersistentIOException {
		ProjectConfigurationDomain domain = null;
		try {
			domain = ProjectConfiguration.getInstance().getProjectConfiguration();
			String path = domain.getXls_destination_path();
			
			File file = new File(path); 
			
//			if(file.exists() == false){
//				file.createNewFile();
//			}else {
//				//delete original file
//				file.delete();
//				file.createNewFile();
//			}
			
			org.apache.poi.hssf.usermodel.HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
			
			
			
			String sheetName = "";
			if(persistentContent != null && persistentContent.size() > 1){
				sheetName = this.getSheetName(persistentContent.get(0).getType(), domain);
			}

			HSSFSheet sheet = workbook.getSheet(sheetName);
			if(sheet == null){
				sheet = workbook.createSheet(sheetName);
			}
			
			for(int i = 0; i < persistentContent.size(); i++){
				OpsAuditLogReportMetaData metaData = persistentContent.get(i);
											
				//print title
				String[] titles = metaData.allFieldName();
				HSSFRow row = sheet.createRow(0);
				for(int x = 0; x < titles.length; x++){
					HSSFCell cell = row.createCell(x, HSSFCell.CELL_TYPE_STRING);
					cell.setCellValue(titles[x]);
				}
				
				//print content
				persistMetaData(metaData,sheet,i + 1);			
			}
			
			System.out.println("sheet name : " + sheetName + " complete...");
			
			FileOutputStream fileOut = new FileOutputStream(file);  
			  
			workbook.write(fileOut);  
			  
			fileOut.close(); 
	
		} catch (NoPropConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidPropertiesException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
//		catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} 
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
	}
	
	public static void main(String[] args){
//		XLSV03PersistentHandler a = new XLSV03PersistentHandler();
//		try {
//			a.persistent(null);
//		
//		} catch (PersistentIOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		try {
			org.apache.poi.hssf.usermodel.HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File("result\\test.xls")),true);
			for(int i = 0; i < 5; i++){
				workbook.createSheet("ffff" + i);
			}
			
			FileOutputStream fileOut = new FileOutputStream(new File("result\\test.xls"));  
			  
			workbook.write(fileOut);  
			  
			fileOut.close();  
//			System.out.println(sheet1);
//			System.out.println(sheet2);
			
			org.apache.poi.hssf.usermodel.HSSFWorkbook workbook1 = new HSSFWorkbook(new FileInputStream(new File("result\\test.xls")),true);
			for(int i = 0; i < 5; i++){
				workbook1.createSheet("eeee" + i);
			}
			FileOutputStream fileOut1 = new FileOutputStream(new File("result\\test.xls"));  
			  
			workbook.write(fileOut1);  
			  
			fileOut.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	private String getSheetName(OpsLogMetaDataType type,ProjectConfigurationDomain domain){
		if(domain == null){
			return null;
		}
		String sheetName = null;
		switch (type){
		case HUB:
			sheetName = domain.getHub_report_name();
			break;
		case INFOSOURCE: 
			sheetName = domain.getInfosource_report_name();
			break;
		case CIR: 
			sheetName = domain.getCir_report_name();
			break;
		case FODS: 
			sheetName = domain.getFods_report_name();
			break;
		case FODS_REJECT: 
			sheetName = domain.getFods_reject_report_name();
			break;
		case FODS_OUTOF_SEQUENCE: 
			sheetName = domain.getFods_outof_seq_report_name();
			break;
		case FODS_NO_ORDER: 
			sheetName = domain.getFods_noorder_report_name();
			break;
		case FODS_EOD: 
			sheetName = domain.getFods_eod_report_name();
			break;
		case FOOT: 
			sheetName = domain.getFoot_report_name();
			break;
		default:
			break;
		}
		return sheetName;
	}
	
	private void persistMetaData(OpsAuditLogReportMetaData data,HSSFSheet sheet,int rowIX){
		String[] content = data.toArray();
		HSSFRow row = sheet.createRow(rowIX);
		switch (data.getType()){
		case HUB:
			for(int i = 0; i < content.length; i++){
				HSSFCell cell = row.createCell(i, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(content[i]);
			}
			break;
		case INFOSOURCE: 
			for(int i = 0; i < content.length; i++){
				HSSFCell cell = row.createCell(i, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(content[i]);
			}
			break;
		case CIR: 
			for(int i = 0; i < content.length; i++){
				HSSFCell cell = row.createCell(i, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(content[i]);
			}
			break;
		case FODS: 
			for(int i = 0; i < content.length; i++){
				HSSFCell cell = row.createCell(i, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(content[i]);
			}
			break;
		case FODS_REJECT: 
			for(int i = 0; i < content.length; i++){
				HSSFCell cell = row.createCell(i, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(content[i]);
			}
			break;
		case FODS_OUTOF_SEQUENCE: 
			for(int i = 0; i < content.length; i++){
				HSSFCell cell = row.createCell(i, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(content[i]);
			}
			break;
		case FODS_NO_ORDER: 
			for(int i = 0; i < content.length; i++){
				HSSFCell cell = row.createCell(i, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(content[i]);
			}
			break;
		case FODS_EOD: 
			for(int i = 0; i < content.length; i++){
				HSSFCell cell = row.createCell(i, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(content[i]);
			}
			break;
		case FOOT: 
			for(int i = 0; i < content.length; i++){
				HSSFCell cell = row.createCell(i, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(content[i]);
			}
			break;
		default:
			break;
		}
	}
}
